<?php

################################################################################################################
Function is_system_mod($mid)
{
	$table_modules = $_SESSION['prefix']."modules";
	$req = dbquery("SELECT type FROM $table_modules WHERE mid='$mid'");
	if (@mysql_num_rows($req) > 0)
	{
		$type = @mysql_result($req, 0, "type");
		if ($type == "system")
			return (TRUE);
	}
	return (FALSE);
}

################################################################################################################
Function is_new_mod($mod)
{
	$table_modules = $_SESSION['prefix']."modules";
	$req = dbquery("SELECT mid FROM $table_modules WHERE path='$mod'");
	if (@mysql_num_rows($req) > 0)
		 return (FALSE);
	return (TRUE);
}

################################################################################################################
Function chk_mod($install_file)
{
	if (!file_exists($install_file))
		return (FALSE);
	require ($install_file);
	if (trim($name) == "")
		return (FALSE);
	if (trim($compatibility) == "")
		return (FALSE);
	return (TRUE);
}

################################################################################################################
function is_compatible($version)
{
	$site = $_SESSION['ver_wup'];
	
	$tmp_site = explode(".", $site);
	$tmp_mod = explode(".", $version);
	if ((int)$tmp_site[0] > (int)$tmp_mod[0])
		return (TRUE);
	elseif ((int)$tmp_site[0] == (int)$tmp_mod[0])
	{
		if ((int)$tmp_site[1] > (int)$tmp_mod[1])
			return (TRUE);
		elseif ((int)$tmp_site[1] == (int)$tmp_mod[1])
		{
			if ((int)$tmp_site[2] > (int)$tmp_mod[2])
				return (TRUE);
			elseif ((int)$tmp_site[2] < (int)$tmp_mod[2])
				return (FALSE);
			else
				return (TRUE);
		}
		else
			return (FALSE);
	}
	else
		return (FALSE);
}

################################################################################################################
Function edit_mod($name_a, $link_a, $show_a, $name_l, $link_l, $show_l, $actif, $actif2, $mid)
{
	if (is_system_mod($mid))
	{
		if (!is_root($_SESSION['login']))
			return (-1);
	}
	
	if (!is_administrator($_SESSION['uid']))
		return (-1);
	
	if ((trim($name_a) == "") || (trim($link_a) == ""))
		$show_a = "ko";
	if ((trim($name_l) == "") || (trim($link_l) == ""))
		$show_l = "ko";
	
	if ($actif == "")
		$actif = $actif2;
		
	$table_modules = $_SESSION['prefix']."modules";
	
	$req = dbquery("UPDATE $table_modules SET name_a='$name_a', link_a='$link_a', show_a='$show_a', name_l='$name_l', link_l='$link_l', show_l='$show_l', actif='$actif' WHERE mid='$mid'");
	if (!$req)
		return (-2);
	return (0);
}

################################################################################################################
Function delete_mod($mid, $type)
{	
	if (is_system_mod($mid))
		return (-1);
		
	if (!is_administrator($_SESSION['uid']))
		return (-1);
		
	if (get_dependency_mod_delete($mid) == FALSE)
		return (-2);
	
	$table_modules = $_SESSION['prefix']."modules";
	$table_modules_rights = $_SESSION['prefix']."modules_rights";
	
	$connection = dbconnect();
	$req = dbquery("SELECT path FROM $table_modules WHERE mid='$mid'", $connection);
	$path = @mysql_result($req, 0, "path");
	$req = dbquery("DELETE FROM $table_modules_rights WHERE mid='$mid'", $connection);
	$req = dbquery("DELETE FROM $table_modules WHERE mid='$mid'", $connection);
	if (!$req)
	{
		@mysql_close($connection);
		return (-3);
	}
	@mysql_close($connection);
	
	if ($type == "user")
	{
		@unlink("../".$path.".php");
		$install_file = "../".$_SESSION['path_apps']."/".$path."/install_mod.php";
		@rename($install_file."_bak", $install_file);
	}
	return (0);
}

################################################################################################################
Function set_rights_mod($mid, $names, $type, $act)
{
	if (!is_administrator($_SESSION['uid']))
		return (-1);
	
	if (is_system_mod($mid))
		return (-1);
	
	if (@$nb = count($names)== 0)
		return (-1);
		
	$table_modules_rights = $_SESSION['prefix']."modules_rights";
	
	@$nb = count($names);
	if (@$nb > 0)
	{
		$i = 0;
		$connection = dbconnect();
		while ($i < $nb)
		{
			if ($act == "rm")
				$req = dbquery("DELETE FROM $table_modules_rights WHERE (mid='$mid' AND type='$type') AND id='$names[$i]'", $connection);
			elseif ($act == "mk")
			{
				$req = dbquery("SELECT rid FROM $table_modules_rights WHERE (mid='$mid' AND type='$type') AND id='$names[$i]'", $connection);
				if (@mysql_num_rows($req) == 0)
				{
					$nid = create_mysql_id("$table_modules_rights", "rid", $connection);
					$req = dbquery("INSERT INTO $table_modules_rights VALUES($nid, '$mid', '$type', '$names[$i]')", $connection);
				}
			}
			$i++;
		}
		mysql_close($connection);
	}
	return (0);
}

################################################################################################################
Function install_mod($mod)
{
	imports("odt_frm_php.users");
	imports("odt_frm_php.mysql");
	
	/*
		-1 No rights
		-2 Old mod
		-3 Bad setting
		-4 No install
		-5 Need dependency
	*/
	
	if (!is_administrator($_SESSION['uid']))
		return (-1);
		
	if (!is_new_mod($mod))
		return (-2);
		
	$install_file = "../".$_SESSION['path_apps']."/".$mod."/install_mod.php";
	if (!chk_mod($install_file))
		return (-3);
	
	$dependency = get_dependency_mod_install($mod);
	if (($dependency != -1) && ($dependency != 0))
	{
		$nb_dependency = count($dependency);
		$depend = "";
		if ($nb_dependency > 0)
		{
			$i = 0;
			while ($i < $nb_dependency)
			{
				if (($get_result = is_new_mod($dependency[$i])) == TRUE)
					return (-5);
				else
				{
					$tmp = get_mid($dependency[$i]);
					if (($i + 1) == $nb_dependency)
						$depend .= $tmp;
					if (($i + 1) < $nb_dependency)
						$depend .= $tmp.",";
				}
				$i++;
			}
		}
	}
	
	if (!(@copy("../index.php", "../".$mod.".php")))
		return (-4);
	
	require ($install_file);
	$table_modules = $_SESSION['prefix']."modules";
	$table_modules_rights = $_SESSION['prefix']."modules_rights";
	$new_place =  create_mysql_id($table_modules, "place");
	
	if (trim(@$show_a) == "")
		$show_a = "ko";
	if (trim(@$show_l) == "")
		$show_l = "ko";
	if (trim(@$link_a) == "")
		$link_a = "$mod.php";
	if (trim(@$link_l) == "")
		$link_l = "$mod.php";
	if (trim(@$name_a) == "")
		$name_a = ucfirst($mod);
	if (trim(@$name_l) == "")
		$name_l = ucfirst($mod);
	
	$connection = dbconnect();
	$nid = create_mysql_id($table_modules, "mid", $connection);
	$rid = create_mysql_id($table_modules_rights, "rid", $connection);
	$req = dbquery("INSERT INTO $table_modules_rights VALUES($rid, '$nid', 'gid', '2')", $connection);
	@$req = dbquery("INSERT INTO $table_modules VALUES($nid, '$name', '$mod', 'ok', '$name_a', '$name_l', '$show_a', '$show_l', '$link_a', '$link_l', '$new_place', 'user', '$depend', '$description', '1.0.0')", $connection);
	if (@mysql_affected_rows() == -1)
	{
		@unlink("../".$mod.".php");
		@mysql_close($connection);
		return (-4);
	}
	@mysql_close($connection);
	
	//SQL Installation
	/*$dir_install = "../".$_SESSION['path_apps']."/".$mod."/module_installation_files/";
	if (is_dir($dir_install))
	{
		$files = get_file_list($dir_install);
		$nb = count($files);
		$i = 0;
		while ($i < $nb)
		{
			if (substr($files[$i], -4) == ".php")
				@copy($dir_install.$files[$i], "../".$files[$i]);
			$i++;
		}
	}*/
	
	@rename($install_file, $install_file."_bak");
	return (0);
}

################################################################################################################
Function add_link($title, $link)
{
	if (!is_administrator($_SESSION['uid']))
		return (-1);
	
	$table_modules = $_SESSION['prefix']."modules";
	$table_modules_rights = $_SESSION['prefix']."modules_rights";
	$new_place =  create_mysql_id($table_modules, "place");
	
	if (trim(@$link) == "")
		$link = "index.php";
	if (trim(@$title) == "")
		$title = "New link";
		
	$title = ucfirst($title);
	
	$connection = dbconnect();
	$nid = create_mysql_id($table_modules, "mid", $connection);
	$rid = create_mysql_id($table_modules_rights, "rid", $connection);
	$req = dbquery("INSERT INTO $table_modules_rights VALUES($rid, '$nid', 'gid', '2')", $connection);
	@$req = dbquery("INSERT INTO $table_modules VALUES($nid, '$title', 'index', 'ok', '$title', '$title', 'ko', 'ko', '$link', '$link', '$new_place', 'link', '', 'Lien utilisateur', '1.0.0')", $connection);
	if (@mysql_affected_rows() == -1)
		return (-1);
	@mysql_close($connection);
	return (0);
}

?>